import i18n from '@/i18n'
import { MetaResourceChartDao } from '@apps/meta/domain/meta-resource-chart/meta-resource-chart.dao'
import { MetaResourceModelDao } from '@apps/meta/domain/meta-resource-model/meta-resource-model.dao'
import { MetaResourceModuleActionDao } from '@apps/meta/domain/meta-resource-module-action/meta-resource-module-action.dao'
import { DomainModel } from '@apps/meta/domain/model'
import { Assist } from '@libs/lhy-common'
import { Body, Controller, Get, Put, Query } from '@nestjs/common'
import { ApiOperation, ApiTags } from '@nestjs/swagger'
import { Prisma } from '@prisma/client'
import { MetaResourceModelBatchSaveDto } from './dto/meta-resource-model-batch-save.dto'
import { MetaResourceModelListByChartDto } from './dto/meta-resource-model-list-by-chart.dto'
import { MetaResourceModelListByModuleDto } from './dto/meta-resource-model-list-by-module.dto'
import { MetaResourceModelListDto } from './dto/meta-resource-model-list.dto'

@ApiTags(i18n.t('app-meta.meta-resource-model.meta-resource-model.12151136-0'))
@Controller({
  path: 'resource_model',
  version: '1',
})
export class MetaResourceModelController {
  constructor(
    private readonly resourceModelDao: MetaResourceModelDao,
    private resourceModuleActionDao: MetaResourceModuleActionDao,
    private resourceChartDao: MetaResourceChartDao
  ) {}

  @ApiOperation({ summary: i18n.t('app-meta.meta-resource-model.meta-resource-model.12151136-1') })
  @Get('list')
  list(@Query() param: MetaResourceModelListDto) {
    return this.resourceModelDao.list({
      relativeId: param.relativeId,
      relativeType: param.relativeType,
      type: param.type,
    })
  }

  @ApiOperation({ summary: i18n.t('app-meta.meta-resource-model.meta-resource-model.12151136-2') })
  @Get('list_by_module')
  async listByModule(@Query() param: MetaResourceModelListByModuleDto) {
    const resourceModuleActions = await this.resourceModuleActionDao.list({
      platform: param.platform,
      name: param.resourceModuleActionName,
      resourceModuleName: param.resourceModuleName,
    })
    const resourceModuleAction = Assist.notNull(
      resourceModuleActions?.[0],
      i18n.t('app-meta.meta-resource-model.meta-resource-model.12151136-3')
    )

    return this.resourceModelDao.list({
      relativeId: resourceModuleAction.id,
      relativeType: DomainModel.MetaResourceRelativeType.ModuleAction,
      type: param.type,
    })
  }

  @ApiOperation({ summary: i18n.t('app-meta.meta-resource-model.meta-resource-model.12151136-4') })
  @Get('list_by_chart')
  async listByChart(@Query() param: MetaResourceModelListByChartDto) {
    const resourceCharts = await this.resourceChartDao.list({
      platform: param.platform,
      name: param.name,
    })

    const resourceChart = Assist.notNull(
      resourceCharts?.[0],
      i18n.t('app-meta.meta-resource-model.meta-resource-model.12151136-5', { args: [param.name] })
    )
    Assist.threw(
      resourceCharts.length > 1,
      i18n.t('app-meta.meta-resource-model.meta-resource-model.12151136-6', {
        args: [param.name, resourceCharts.length],
      })
    )

    return this.resourceModelDao.list({
      relativeId: resourceChart.id,
      relativeType: DomainModel.MetaResourceRelativeType.Chart,
      type: DomainModel.MetaResourceModelType.Request,
    })
  }

  @ApiOperation({ summary: i18n.t('app-meta.meta-resource-model.meta-resource-model.12151136-7') })
  @Put()
  async batchSave(@Body() param: MetaResourceModelBatchSaveDto): Promise<void> {
    // 校验数据
    const itemNames: string[] = []
    const itemLabels: string[] = []
    const models: Prisma.MetaResourceModelCreateManyInput[] = []
    param.items.forEach(item => {
      Assist.threw(
        itemNames.includes(item.name),
        i18n.t('app-meta.meta-resource-model.meta-resource-model.12151136-8', { args: [item.name] })
      )
      Assist.threw(
        itemLabels.includes(item.label),
        i18n.t('app-meta.meta-resource-model.meta-resource-model.12151136-9', { args: [item.label] })
      )

      itemNames.push(item.name)
      itemLabels.push(item.label)
      models.push({
        ...item,
        type: param.type,
        relativeType: param.relativeType,
        relativeId: param.relativeId,
      })
    })

    // 删除旧数据
    await this.resourceModelDao.deleteByRelative({
      relativeType: param.relativeType,
      relativeId: param.relativeId,
      type: param.type,
    })

    // 添加新数据
    await this.resourceModelDao.batchSave(models)
  }
}
